package com.example.myapplication;


import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class keyutils {
    private final Keyboard k1; //自定义的键盘
    private KeyboardView keyboardView;
    private EditText editText;


    public keyutils(Keyboard k1, KeyboardView keyboardView, EditText editText) {
        this.k1 = k1;
        this.keyboardView = keyboardView;
        this.editText = editText;
        this.editText.setInputType(InputType.TYPE_NULL);//取消弹出框
        //放入内容和我们定义好的组件
        k1 = new Keyboard(this.editText.getContext(), R.xml.key);
        //设置键盘样式
        this.keyboardView.setKeyboard(k1);
        this.keyboardView.setEnabled(true);
        this.keyboardView.setPreviewEnabled(false);
        this.keyboardView.setOnKeyboardActionListener(listener);//设置键盘按钮被点击了的监听

    }

    interface OnEnsureListener {
        public void onEnsure();
    }

    OnEnsureListener onEnsureListener;

    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }

    KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {
        @Override
        public void onPress(int i) {

        }

        @Override
        public void onRelease(int i) {

        }

        @Override
        public void onKey(int i, int[] ints) {
            Editable editable = editText.getText();
            int start = editText.getSelectionStart();
            switch (i) {
                case Keyboard.KEYCODE_DELETE:   //点击了删除键
                    if (editable != null && editable.length() > 1) {
                        if (start > 0) {
                            editable.delete(start - 1, start);
                        }
                    }else {
                        editable.clear();
                        editable.insert(0, "0");
                    }
                    break;
                case Keyboard.KEYCODE_CANCEL:   //点击了清零
                    editable.clear();
                    editable.insert(0, "0");
                    break;
                case Keyboard.KEYCODE_DONE:    //点击了完成
                    onEnsureListener.onEnsure();   //通过接口回调的方法，当点击确定时，可以调用这个方法
                    break;
                case 46:  //点击了小数点
                    if (editable.toString().contains(".")||editable.length()==0) {
                        return;
                    }else {
                        editable.insert(start, Character.toString((char) i));
                        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(editable.length()+3)});
                    }
                    break;
                default:  //其他数字直接插入
                    if (editable.toString().equals("0.00")||editable.toString().equals("0.0")||editable.toString().equals("0")){
                        editable.clear();
                        start=0;
                    }
                    if (editable.toString().contains(".")&&editable.toString().indexOf(".")<start&&editable.toString().substring(editable.toString().indexOf(".")).length()>2){
                        return;
                    }else{
                        editable.insert(start, Character.toString((char) i));
                    }
                    break;

            }


        }

        @Override
        public void onText(CharSequence charSequence) {

        }

        @Override
        public void swipeLeft() {

        }

        @Override
        public void swipeRight() {

        }

        @Override
        public void swipeDown() {

        }

        @Override
        public void swipeUp() {

        }
    };

    //    显示键盘
    public void showKey() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.INVISIBLE || visibility == View.GONE) {
            keyboardView.setVisibility(View.VISIBLE);
        }
    }

    //    隐藏键盘
    public void hideKeyboard() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.VISIBLE || visibility == View.INVISIBLE) {
            keyboardView.setVisibility(View.GONE);
        }
    }
}


